//
//  UIImage+boundle.m
//  WSComponent
//
//  Created by lienbao on 11/09/2014.
//  Copyright (c) 2014 Lienbao. All rights reserved.
//

#import "UIImage+bundle.h"

@implementation UIImage (boundle)

+ (NSString *)pathForResource:(NSString *)imageName bundle:(NSString *)bundle
{
    NSString *bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:bundle];
    NSBundle *resBundle = [NSBundle bundleWithPath:bundlePath];
    NSString *path = [resBundle pathForResource:imageName ofType:@"png"];
    return path;
}

+ (UIImage *)bundleImage:(NSString *)imageName
{
    return [self bundleImage:imageName bundle:@"UIComponent.bundle"];
}

+ (UIImage *)bundleWeiImage:(NSString *)imageName
{
    return [self bundleImage:imageName bundle:@"wswei64.bundle"];
}

+ (UIImage *)bundleImage:(NSString *)imageName bundle:(NSString *)bundle
{
    CGFloat scale = [[UIScreen mainScreen] scale];
    NSString *scaleImage =  [NSString stringWithFormat:@"%@@%.0fx", imageName, scale];
    NSString *path = [self pathForResource:scaleImage bundle:bundle];
    if (path) {
        return [UIImage imageWithContentsOfFile:path];
    }
    scaleImage =  [NSString stringWithFormat:@"%@@2x", imageName];
    path = [self pathForResource:scaleImage bundle:bundle];
    return [UIImage imageWithContentsOfFile:path];
}

@end
